package org.wishwork.api.stock.in.box;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.wishwork.api.Api;
import org.wishwork.api.stock.in.box.request.Item;
import org.wishwork.api.stock.in.box.request.StockInBoxRequest;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 采购按箱入库
 * @author: WangYX
 * @create: 2022-04-11 20:42
 * @Version: 1.0.0
 **/

public class StockInBoxApi extends Api<StockInBoxResponse, List<StockInBoxRequest>> {

    private Context context;

    public StockInBoxApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(List<StockInBoxRequest> stockInBoxRequests) throws Exception {
        String url = "/open/purchasein/confirm/upload";
        List<Map<String, Object>> data = new ArrayList<>();
        for (StockInBoxRequest stockInBoxRequest : stockInBoxRequests) {
            Map<String, Object> params = new HashMap<>();
            if (stockInBoxRequest.getWmsCoId() != null) {
                params.put("wms_co_id", stockInBoxRequest.getWmsCoId());
            }
            params.put("po_id", stockInBoxRequest.getSupplierId());
            if (StrUtil.isNotEmpty(stockInBoxRequest.getPoId())) {
                params.put("supplier_id", stockInBoxRequest.getPoId());
            }
            params.put("so_id", stockInBoxRequest.getSoId());

            if (stockInBoxRequest.getItems() != null && stockInBoxRequest.getItems().size() > 0) {
                List<Map<String, Object>> items = new ArrayList<>();
                for (Item item : stockInBoxRequest.getItems()) {
                    Map<String, Object> map = new HashMap<>();
                    if (StrUtil.isNotEmpty(item.getPackId())) {
                        map.put("pack_id", item.getPackId());
                    }
                    map.put("sku_id", item.getSkuId());
                    map.put("qty", item.getQty());
                    if (StrUtil.isNotEmpty(item.getSkuSn())) {
                        map.put("sku_sn", item.getSkuSn());
                    }
                    items.add(map);
                }
                params.put("items", items);
            }
            data.add(params);
        }
        String biz = JSON.toJSONString(data);
        return HttpHelper.sendRequest(biz, url);
    }

    @Override
    protected StockInBoxResponse process(JSONObject data, List<StockInBoxRequest> stockInBoxRequests) throws Exception {
        return null;
    }
}
